package com.guohui.boot1.config.datasources;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import com.alibaba.druid.pool.DruidDataSource;

@Configuration
@ConditionalOnClass(com.alibaba.druid.pool.DruidDataSource.class)
@ConditionalOnProperty(name = "spring.datasource.type", havingValue = "com.alibaba.druid.pool.DruidDataSource", matchIfMissing = true)
@MapperScan("com.guohui.boot1.dao")//全局扫描mybatis的mapper接口
public class DataSourceConfiguration {
	
	private Logger logger = LoggerFactory.getLogger(DataSourceConfiguration.class);

	@Autowired
	private DruidDataSourceProperty druidDataSourceProperty;

	@Bean
	@Primary  //如果未显示声明注入DataSource，怎会默认使用此被标注的DataSource
	public DataSource dataSource() {
	  DruidDataSource datasource = new DruidDataSource();
	  
	  datasource.setUrl(druidDataSourceProperty.getUrl());
	  datasource.setUsername(druidDataSourceProperty.getUsername());
	  datasource.setPassword(druidDataSourceProperty.getPassword());
	  datasource.setDriverClassName(druidDataSourceProperty.getDriverClassName());
	  
	  datasource.setInitialSize(druidDataSourceProperty.getInitialSize());
	  datasource.setMinIdle(druidDataSourceProperty.getMinIdle());
	  datasource.setMaxActive(druidDataSourceProperty.getMaxActive());
	  // 配置获取连接等待超时的时间
	  datasource.setMaxWait(druidDataSourceProperty.getMaxWait());
	  // 配置间隔多久才进行一次检测，检测需要关闭的空闲连接，单位是毫秒
	  datasource.setTimeBetweenEvictionRunsMillis(druidDataSourceProperty.getTimeBetweenEvictionRunsMillis());
	  // 配置一个连接在池中最小生存的时间，单位是毫秒
	  datasource.setMinEvictableIdleTimeMillis(druidDataSourceProperty.getMinEvictableIdleTimeMillis());
	  datasource.setValidationQuery(druidDataSourceProperty.getValidationQuery());
	  datasource.setTestWhileIdle(druidDataSourceProperty.isTestWhileIdle());
	  datasource.setTestOnBorrow(druidDataSourceProperty.isTestOnBorrow());
	  datasource.setTestOnReturn(druidDataSourceProperty.isTestOnReturn());
	  datasource.setPoolPreparedStatements(druidDataSourceProperty.isPoolPreparedStatements());
	  datasource.setMaxPoolPreparedStatementPerConnectionSize(
	    druidDataSourceProperty.getMaxPoolPreparedStatementPerConnectionSize());
	  datasource.setUseGlobalDataSourceStat(druidDataSourceProperty.isUseGlobalDataSourceStat());
	  try {
	    datasource.setFilters(druidDataSourceProperty.getFilters());
	  } catch (SQLException e) {
	    logger.error("druid configuration initialization filter", e);
	  }
	  datasource.setConnectionProperties(druidDataSourceProperty.getConnectionProperties());
	  return datasource;
	}
}
